<?php
/**
 * @分销
 * @功能说明:
 * @更新说明: 暂无更新
 * @文件名: KaolaController.class.php
 * @编码: UTF-8
 * @创建时间: 2016-03-22 13:32
 * @创建人: WENCUN
 */

namespace dg\Controller;


class KaolaController extends ApibaseController
{
    /*
     * 考拉商品列表
     * @date: 2016-03-22 13:33:00
     * @editor: WENCUN
     * */
    public function index(){
        $page = I('get.page') ?:1;
        $parpage = 10;
        $url = array();
        $list = M('GoodsKlwait');
        //统计品牌
        $this->brand = $list->distinct(true)->field(array('brand_name'))->select();
        if(I('get.goods_title')){
            $list = $list->where('goods_title LIKE "%'.I('get.goods_title').'%"');
            $url['goods_title'] = I('get.goods_title');
        }
        if(I('get.brand_name')){
            $list = $list->where(array('brand_name'=>I('get.brand_name')));
            $url['brand_name'] = I('get.brand_name');
        }
        if(I('get.status')!=''){
            if(I('get.status')==1){
                $list = $list->where('goods_id != 0');
            }else{
                $list = $list->where('goods_id = 0');
            }
            $url['status'] = I('get.status');
        }
        $object = clone $list;
        $getData = $list->order(array('add_at'=>'DESC'))->page($page,$parpage)->select();
        $this->assign('page',setPage($object->count(),$parpage,$page,'?'.http_build_query($url)));
        $this->assign('getData',$getData);
        $this->display('/kaola/index');
    }

    public function klGoods(){
        $param = self::sign();
        //获取渠道商所有的商品SkuID
        $content = curl('http://223.252.220.85/api/queryAllGoodsId',$param);
        $data = json_decode($content,1);
        //根据SkuID获取所有商品的数据
        foreach($data['goodsInfo'] as $key=>$value){
            $param2 = self::sign($value);
            $cont = curl('http://223.252.220.85/api/queryGoodsInfoById',$param2);
            $goodsData[$key] = json_decode($cont,1)['goodsInfo'];
        }
        //商品数据重组
        foreach($goodsData as $key=>$value){
            $newGoods[$key] = array(
                'kl_goods_id' => $value['goodsId'],
                'sku_id' => $value['skuId'],
                'brand_name' => $value['brandName'],
                'goods_price' => $value['price'],
                'goods_title' => $value['title'],
                'goods_img' => $value['imageUrl'],
                'store' => $value['store'],
                'brand_country' => $value['brandCountryName'],
                'goods_pic' => $value['goodsImages'][0]['imageUrl'],
                'goods_pic2' => $value['goodsImages'][1]['imageUrl']?:'',
                'goods_pic3' => $value['goodsImages'][2]['imageUrl']?:'',
                'goods_detail' => htmlspecialchars($value['detail']),
                'storage' => $value['storage'],
                'add_at' => time(),
                'update_at' => time()
            );
        }

        //写入数据库
        foreach($newGoods as $key=>$value){
            //检查商品是否已经在数据库中
            if(!M('GoodsKlwait')->where(array('sku_id'=>$value['sku_id']))->count()){
                M('GoodsKlwait')->add($newGoods[$key]);
            }
        }

    }

    /*
     * 考拉海购API签名
     * @date: 2016-03-23 16:52:00
     * @editor: WENCUN
     * */
    static function sign($skuId=null){
        $secretKey = 'bfa9602ec39e1d18ef434cbdfe9ff0ad25038ad94fca338314ed04d4b4688f58';
        $appKey = '417635e229aa376bdff8d17c50fd15171f034584e550986eb1ededc930f3633a';
        $httpHeader = array(
            'channelId' => '1100',
            'timestamp' => date('Y-m-d H:i:s'),
            'app_key' => '417635e229aa376bdff8d17c50fd15171f034584e550986eb1ededc930f3633a',
            'v' => '1.0',
            'sign' => strtoupper(md5($secretKey.'app_key'.$appKey.'timestamp'.date('Y-m-d H:i:s').'v1.0'.$secretKey)),
            'sign_method' => 'md5',
        );
        if($skuId!=null){
            $httpHeader['skuId'] = $skuId;
            $httpHeader['queryType'] = '0';
        }
        return $httpHeader;
    }

    /*
     * 考拉商品编辑
     * @date: 2016-03-22 15:39:00
     * @editor: WENCUN
     * */
    public function goodsEdit(){
        if(IS_POST){
            if(I('post.dosubmit')==1 && I('post.goods_id')){
                $GoodsId = M('GoodsKlwait')->field(array('goods_id'))->where(array('id'=>I('post.goods_id')))->find();
                if(!empty($GoodsId['goods_id'])){
                    $this->showtips('该商品已入库');exit;
                }
                $data = array(
                    'goods_title' => trim(I('post.goods_title')),
                    'goods_price' => trim(I('post.goods_price')),
                    'channel_price' => trim(I('post.channel_price')),
                    'goods_attr' => trim(I('post.goods_attr')),
                    'kcun' => trim(I('post.kcun')),
                    'goods_pic' => trim(I('post.goods_pic')),
                    'goods_pic2' => trim(I('post.goods_pic2')),
                    'goods_pic3' => trim(I('post.goods_pic3')),
                    'hjprofit' => trim(I('post.hjprofit')),
                    'byprofit' => trim(I('post.byprofit')),
                    'zsprofit' => trim(I('post.zsprofit')),
                    'cate1_id' => trim(I('post.cate1_id')),
                    'cate2_id' => trim(I('post.cate2_id')),
                    'status' => trim(I('post.status')),
                    'type' => '1',
                    'goods_content' => htmlspecialchars(I('post.content')),
                    'add_at' => time(),
                    'update_at' => time()
                );
                $addId = M('GoodsEntity')->add($data);
                if($addId){
                    putlog('入库商品'.I('post.goods_title').'`ID'.$addId.'`','KaolaController','goodsEdit');
                    M('GoodsKlwait')->where(array('id'=>I('post.goods_id')))->setField(array('goods_id'=>$addId));
                    $this->showtips('入库商品成功');
                }else{
                    $this->showtips('操作失败');
                }
            }
        }
        if(I('get.id')){
            $getData = M('GoodsKlwait')->where(array('id'=>I('get.id')))->find();
            //获取分类信息
            $cateMsg = M('CateEntity')->where(array('parent_id'=>0))->select();
            $this->assign('cate',$cateMsg);
            $this->assign('getData',$getData);
            $this->display('/kaola/goodsedit');
        }
    }

}